package as3.component.controls
{
	import as3.component.Component;
	import as3.component.support.PanelTitle;
	import as3.style.Margin;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class Panel extends Component
	{
		
		protected var _bg:DisplayObject;
		
		protected var _panelTitle:PanelTitle;
		
		private var _dragabled:Boolean = true;
		private var beginP:Point = new Point(0,0);
		
		private var _isModel:Boolean = false;
		
		private var _title:String;
		
		public var panelType:Object;
		
		protected var isShowBag:Boolean = true;
		
		protected var contactPanelType:Object;

		/**面板周边透明色空间*/
		public const MARGIN_TRANSPARENT:Margin = new Margin(7, 7, 7, 7);
		public const MARGIN:int = 7;
		
		public function Panel(type:Object = null, args:Array = null)
		{
			panelType = type;
			super();
		}
		
		override protected function initUI():void
		{
			super.initUI();
			_uiLibrary["bg"] = "PanelBackgroundSkin";
		}
		
		override protected function initCom():void
		{
			super.initCom();
			_panelTitle = new PanelTitle(panelType.title);
			addChild(_panelTitle);
			_panelTitle.addEventListener(Event.CLOSE, onCloseHandler);
			$addListener();
		}
		
		public function set bg(dis:DisplayObject):void
		{
			if (dis && _bg != dis) 
			{
				_bg = dis;
				setBackgroundView(_bg);
			}
		}
		
		public function onOpen():void
		{
			setDragabled(_dragabled);
			if (contactPanelType) 
			{
				//PanelManager.getInstance().open(contactPanelType);
			}
			show();
		}
		
		/**
		 * 打开前的处理 
		 * 
		 */
		public function preOpen():void
		{
			
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
			_bg.width = value;
			if (_panelTitle) _panelTitle.width = value;
			propertyChanged = true;
		}
		
		override public function get width():Number
		{
			return _width;
		}
		
		override public function set height(value:Number):void
		{
			_height = value;
			_bg.height = value;
			propertyChanged = true;
		}
		
		override public function get height():Number
		{
			return _height;
		}
		
		protected function sizeChange():void
		{
			if (_bg != null && isShowBag == true) {
				graphics.clear();
//				graphics.beginBitmapFill(PanelManager.getInstance().getBgBitmapdata(), null, true);
				graphics.drawRect(2, 25, width - 4, height - 45);
				graphics.endFill();
			}
		}
		
		public function set title(value:String):void
		{
			if (value == _title) return;
			_title = value;
			_panelTitle.title = _title;
		}
		
		public function get title():String
		{
			return _title;
		}
		
		private var _isOpen:Boolean = false;

		/**面板是否打开*/
		public function get isOpen():Boolean
		{
			return _isOpen;
		}

		public function setDragabled(value:Boolean):void
		{
			if (value != _dragabled) {
				_dragabled = value;
				if (value) {
					$addListener();
				} else {
					$removeListener();
				}
			}
		}
		
		private function $addListener():void
		{
			_panelTitle.addEventListener(MouseEvent.MOUSE_UP, onPanelClickUp);
			_panelTitle.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag, false, 0, true);
		}
		
		private function $removeListener():void
		{
			if(_panelTitle)
			{
				_panelTitle.removeEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
			}
		}
		
		private function onPanelClickUp(evt:MouseEvent):void
		{
			//PanelManager.getInstance().bringToUp(this);
		}
		
		private function onDrag(evt:MouseEvent):void
		{
			var newX:Number = stage.mouseX;
			if (stage.mouseX > stage.stageWidth) newX = stage.stageWidth;
			newX = newX < 0 ? 0 : newX;
			var newY:Number = stage.mouseY
			if (stage.mouseY > stage.stageHeight) newY = stage.stageHeight;
			newY = newY < 0 ? 0 : newY;
			x = Math.ceil(newX - beginP.x);
			y = Math.ceil(newY - beginP.y);
			evt.updateAfterEvent();
			_move();
		}
		
		private function beginDrag(evt:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onDrag);
			stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
			beginP.x = stage.mouseX - x;
			beginP.y = stage.mouseY - y;
			cacheAsBitmap = true;
		}
		
		private function endDrag(evt:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDrag);
			stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
			cacheAsBitmap = false;
			x = Math.ceil(x);
			y = Math.ceil(y);
		}
		
		/**
		 * 想监听面板移动覆盖此方法即可
		 */		
		public function _move():void
		{
		
		}
		
		public function onResize():void
		{
			return;
		}
		
		public function set isModel(value:Boolean):void
		{
			if (value == _isModel) return;
			_isModel = value;
		}
		
		public function get isModel():Boolean
		{
			return _isModel;
		}
		
		protected function setRect(target:DisplayObject, _continer:DisplayObjectContainer, $y:Number, $w:Number, $h:Number):void
		{
			target.y = $y;
			target.x = Math.ceil((_continer.width - $w) * 0.5);
			target.width = $w;
			target.height = $h;
			_continer.addChild(target);
		}
		
		protected function setButtons(... args):void
		{
			var _w:Number = 0;
			var i:int;
			for (i = 0; i < args.length; i++) {
				_w = (i +1) * args[i].width + (i * 8);
			}
			var baseX:Number = (width - _w) * 0.5;
			for (i = 0; i < args.length; i++) {
				args[i].x = baseX + (i * (args[i].width + 8));
				args[i].y = height - 42;
				addChild(args[i]);
			}
		}
		
		private function onCloseHandler(evt:Event):void
		{
			close();
		}
		
		public function close(evt:MouseEvent = null):void
		{
//			onClose();/**在PanelManager.getInstance().close里有*/
			//PanelManager.getInstance().close(panelType);
		}
		
		public function onClose():void
		{
			hide();
		}
		
		/**
		 * 面板隐藏时调用
		 */		
		public function hide():void
		{
			_isOpen = false;
		}
		
		/**
		 * 面板显示时调用
		 */		
		public function show():void
		{
			_isOpen = true;
		}
		
		override public function updateDisplay():void
		{
			sizeChange();
		}
		
		public function clear():void
		{
			$removeListener();
			panelType = null;
			if (_panelTitle) {
				_panelTitle.removeEventListener(Event.CLOSE, onCloseHandler);
				_panelTitle.removeEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
				removeChild(_panelTitle);
				_panelTitle = null;
			}
			setBackgroundView(null);
			_bg = null;
			var len:int = numChildren;
			
			while(len > 0)
			{
				removeChildAt(0);
				len--;
			}
		}
		
		private var _bMovable:Boolean = true;
		public function set bMovable(value:Boolean):void
		{
			if(_bMovable != value)
			{
				_bMovable = value;
				
				if(_bMovable)
					_panelTitle.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag, false, 0, true);
				else
					_panelTitle.removeEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
			}
		}
		
		public function setCloseBtnVisible(v:Boolean):void
		{
			this._panelTitle._closeBtn.visible = v;
		}
		
		public function setTitleVisible(v:Boolean):void
		{
			this._panelTitle.visible = v;
		}
	}
}